package com.managertrade.controller;

import com.managertrade.common.Result;
import com.managertrade.dao.sqlite.entity.KlineWithIndicatorsEntity;
import com.managertrade.dao.sqlite.entity.LocalLog;
import com.managertrade.dto.SysUserDTO;
import com.managertrade.dao.mysql.entity.SysRole;
import com.managertrade.service.impl.SysUserService;
import com.managertrade.service.sqllite.service.KlineWithIndicatorsService;
import com.managertrade.service.sqllite.service.LocalLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.util.StopWatch;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@CrossOrigin // 支持跨域请求
@RestController
@RequestMapping("/api/users")
@Slf4j
public class UserController {

    @Autowired
    private SysUserService sysUserService;
    // 获取用户分页列表
    @GetMapping
    public Result<Page<SysUserDTO>> list(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(required = false) String username) {
        StopWatch stopWatch =  new StopWatch();
        stopWatch.start();
        Pageable pageable = PageRequest.of(page, size, Sort.by("id").descending());
        Page<SysUserDTO> users = sysUserService.getUsers(pageable, username);
        if(!users.isEmpty()){
            Map<Long, SysRole> userRolesMap = sysUserService.getUserRolesMap(users.get().map(k -> k.getId()).collect(Collectors.toList()));
            List<SysUserDTO> collect = users.get().collect(Collectors.toList());
            for(SysUserDTO sysUserDTO :collect){
                if(userRolesMap.get(sysUserDTO.getId())!=null){
                    sysUserDTO.setRoleId(userRolesMap.get(sysUserDTO.getId()).getId());
                    sysUserDTO.setRoleName(userRolesMap.get(sysUserDTO.getId()).getName());
                }
            }
        }
        stopWatch.stop();
        log.info("用户分页列表耗时：{}ms",stopWatch.getTotalTimeMillis());
        return Result.success(users);
    }

    // 添加用户
    @PostMapping
    public Result<?> add(@RequestBody SysUserDTO user) {
        sysUserService.save(user);
        return Result.success();
    }

    // 更新用户
    @PutMapping
    public Result<?> update(@RequestBody SysUserDTO user) {
        sysUserService.updateById(user);
        return Result.success();
    }

    // 删除用户
    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Long id) {
         sysUserService.deleteById(id);
        return Result.success();
    }

    // 重置密码
    @PutMapping("/resetPassword/{id}")
    public Result<?> resetPassword(@PathVariable Long id) {
        sysUserService.resetPassword(id);
        return Result.success();
    }
}
